package com.tbs.parkingsaleapp.UI.Adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.tbs.parkingsaleapp.DataTool.Cal;
import com.tbs.parkingsaleapp.DataTool.RecommendationCalculator;
import com.tbs.parkingsaleapp.Http.Actionner.ADResulter;
import com.tbs.parkingsaleapp.Http.JsonData.ParkResult;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.SystemManager.SystemDataMananger;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;

public class ParkAdapter extends BaseAdapter {

    Context context;
    static ArrayList<ParkResult> arrayList;

    public static ParkResult get(int pos)
    {
        return arrayList.get(pos);
    }

     public ParkAdapter(Context context, ArrayList<ParkResult> r)
     {
         this.context=context;
         arrayList=r;
     }
    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return arrayList.get(position).getPark().getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

         View root=View.inflate(context, R.layout.simpleparkitem,null);
        Banner banner=root.findViewById(R.id.prk_pic);
        ParkResult parkResult=arrayList.get(position);
        ArrayList<String> imgs=new ArrayList<>();
        for (String s:parkResult.getImgPaths()
        ) {
            imgs.add(s);
        }
        banner.setImageLoader(new ADResulter.myImgLoader());
        banner.setImages(imgs);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setBannerAnimation(Transformer.DepthPage);
        banner.isAutoPlay(true);
        banner.setDelayTime(15000);
        banner.start();

        TextView textView=root.findViewById(R.id.prk_title);
        textView.setText(String.format("%s的%d号车位", SystemDataMananger.FindLocation(parkResult.getPark().getLocation()),parkResult.getPark().getNumber()));
        textView=root.findViewById(R.id.prk_info);
        boolean buyalbe= SystemDataMananger.Buyable(parkResult);

        String v="";
        if(!buyalbe)
        {
            textView.setBackgroundColor(context.getColor(R.color.banBuy));
            v="您没有购买权限";
            textView.setText(String.format("%s\n%s",parkResult.getPark().getNote(),v));
        }
        else
        {
          ParkResult.RecommondData data= parkResult.Recommond();
           v=String.format("平均推荐度:%.2f\n最近小区距离:%.2fkm",data.avg,
                    RecommendationCalculator.fromUser(SystemDataMananger.FindLocation(parkResult.getPark().getLocation()).getLocation()));
            textView.setText(String.format("%s\n%s",parkResult.getPark().getNote(),v));
            textView=root.findViewById(R.id.prk_rcm);
            textView.setText(String.format("购买成功可能推荐度:%.2f\n行程距离推荐度：%.2f\n价格推荐度：%.2f\n喜好推荐度:%.2f",data.raw[Cal.rcm_ORDER],
                    data.raw[Cal.rcm_DISTANCE],
                    data.raw[Cal.rcm_PRICE],
                    data.raw[Cal.rcm_LIKE]));
        }
        textView=root.findViewById(R.id.prk_price);
        textView.setText(String.format("%.2f 元",parkResult.getPark().getPrice()));


        return root;
    }
}
